function selectProvidersToGeocode(selectedProviderIds) {

    //Define the Global Variables across functions.
    var bingMapsKey = 'AnMPbAuzGgURbEiN9H96j4mBiJo-JPk4fqMtkmfc6NEet28pqceuyiqk_kWrFD6x';
    var orgUrl = window.parent.Xrm.Page.context.getClientUrl();
    var webAPIUrl = "/api/data/v8.1/";
    var options =
        "?$select=name,address1_line1,address1_city,address1_stateorprovince,address1_postalcode,address1_country,address1_latitude,address1_longitude,ppms_geocoded&$filter=";
    var requestType = "accounts";

    debugger;
    //Define the Global Variables across functions.
    var bingMapsKey = 'AnMPbAuzGgURbEiN9H96j4mBiJo-JPk4fqMtkmfc6NEet28pqceuyiqk_kWrFD6x';
    var orgUrl = window.parent.Xrm.Page.context.getClientUrl();
    var webAPIUrl = "/api/data/v8.1/";
    var options =
        "?$select=name,address1_line1,address1_city,address1_stateorprovince,address1_postalcode,address1_country,address1_latitude,address1_longitude,ppms_geocoded&$filter=";
    var requestType = "accounts";

    for (var i = 0; i < selectedProviderIds.length; i++) {
        //Format the ID to remove braces
        var ProviderId = selectedProviderIds[i].toString();
        ProviderId = ProviderId.replace(/[{}]/g, "");

        options += "(accountid eq " + ProviderId + ")"
        if (i != selectedProviderIds.length - 1) {
            options += " or ";
        }
    }

    if (selectedProviderIds.length <= 250) {
        RetrieveProviders(options);
    }
    else{return};


    function RetrieveProviders(options) {

        var request = new String();

        request += orgUrl + webAPIUrl + requestType + options;

        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: request,
            beforeSend: function(XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.             
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function(data, textStatus, xhr) {
                verifyGeocodeNeeded(data, textStatus, xhr);
            },
            error: function(e) {
                showErrorMsg(e.statusText);
            }
        });
    };

    function verifyGeocodeNeeded(data, textStatus, xhr) {
        var providers = data.value;
        if (providers.length > 0) {
            filteredProviders = new Array();
            for (var p = 0; p < providers.length; p++) {
                if (providers[p].address1_latitude == null && providers[p].address1_longitude == null) {
                    filteredProviders.push(providers[p])
                }
            }
            providers = filteredProviders
        }
        if (providers.length > 0) {
            geoCodeProviders(providers)
        };
    };

    function geoCodeProviders(providers) {

        for (var p = 0; p < providers.length; p++) {

            var providerId = providers[p].accountid;

            var providerAddress = new String;
            //Skipping the Street Address for Test data as many of the Test Records have fake street names
            providerAddress += providers[p].address1_line1 + ", ";
            providerAddress += providers[p].address1_city + " ";
            providerAddress += providers[p].address1_stateorprovince + " ";
            providerAddress += providers[p].address1_postalcode;

            //Call the Bing Maps REST Services and provide the address which needs to be Geocoded. Returned Result will have the Lat & Long coordinates. 
            var geocodeRequest = "https://dev.virtualearth.net/REST/v1/Locations?q=" +
                encodeURIComponent(providerAddress) +
                "&key=" +
                encodeURIComponent(bingMapsKey);
            CallRestService(providerId, geocodeRequest);
        }
    }

    function CallRestService(providerId, geocodeRequest) {
        $.ajax({
            url: geocodeRequest,
            dataType: "jsonp",
            async: true,
            jsonp: "jsonp",
            success: function(r) {
                GeocodeCallBack(r, providerId);
            },
            error: function(e) {
                alert(e.statusText);
            }
        });
    }

    function GeocodeCallBack(result, providerId) {
        // Do something with the result
        var lat = result.resourceSets[0].resources[0].geocodePoints[0].coordinates[0];
        var long = result.resourceSets[0].resources[0].geocodePoints[0].coordinates[1];
        updateProviderCoordinates(lat, long, providerId);
    }

    function updateProviderCoordinates(lat, long, providerId) {

        var clientUrl = Xrm.Page.context.getClientUrl();
        var req = new XMLHttpRequest()
        req.open("PATCH",
            encodeURI(orgUrl + webAPIUrl + requestType + "(" + providerId + ")"),
            true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");

        var body = JSON.stringify({
            "address1_latitude": lat,
            "address1_longitude": long,
            "ppms_geocoded": true
        });
        req.send(body);

        //alert("Provider Geocoded Successfully")
    };


}
